package com.wei.czz.framework.store.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wei.czz.common.enums.CommonEnum;
import com.wei.czz.common.po.UserPo;
import com.wei.czz.common.utils.SecurityUtils;
import com.wei.czz.common.vo.store.audit.AuditSaveVo;
import com.wei.czz.framework.store.dao.AuditStoreDao;
import com.wei.czz.framework.store.entity.AuditStoreEntity;
import com.wei.czz.framework.store.service.AuditStoreService;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.util.*;

/**
 * Created by IntelliJ IDEA.
 *
 * @author wei
 * date: 2023-12-01 11:38:26
 * className: AuditStoreServiceImpl
 * version: 1.0
 * description:
 */
@Service("auditStoreService")
public class AuditStoreServiceImpl extends ServiceImpl<AuditStoreDao, AuditStoreEntity> implements AuditStoreService {

    private static final Logger log = LoggerFactory.getLogger(AuditStoreServiceImpl.class);

    @Override
    public Integer saveAuditStore(AuditSaveVo auditSaveVo) {

        AuditStoreEntity auditStore = this.buildAuditStore(auditSaveVo);
        // 添加
        int count = baseMapper.insert(auditStore);
        log.info("保存商店审核记录完成。count={}", count);
        return count;
    }

    @Override
    public List<AuditStoreEntity> getAuditStoreList(Collection<Long> userIdCollection, Date createTime) {
        if (userIdCollection.isEmpty()) {
            log.info("传入的用户主键集合为空");
            return Collections.emptyList();
        }
        LambdaQueryWrapper<AuditStoreEntity> auditStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 匹配条件
        auditStoreLambdaQueryWrapper.in(AuditStoreEntity::getUserId, userIdCollection)
                .eq(Objects.nonNull(createTime), AuditStoreEntity::getCreateTime, createTime)
                .eq(AuditStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<AuditStoreEntity> auditStoreList = baseMapper.selectList(auditStoreLambdaQueryWrapper);
        if (auditStoreList.isEmpty()) {
            log.info("商店审核记录列表为空。userIdCollection={}", userIdCollection);
        }
        return auditStoreList;
    }

    @Override
    public List<AuditStoreEntity> getAuditStoreList(Long businessId, Long accountId, Long bossId) {
        if (ObjectUtils.allNull(businessId, accountId, bossId)) {
            log.info("传入参数均为空");
            return Collections.emptyList();
        }

        LambdaQueryWrapper<AuditStoreEntity> auditStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        // 匹配条件
        auditStoreLambdaQueryWrapper.in(Objects.nonNull(businessId), AuditStoreEntity::getBusinessId, businessId)
                .eq(Objects.nonNull(accountId), AuditStoreEntity::getAccountId, accountId)
                .eq(Objects.nonNull(bossId), AuditStoreEntity::getBossId, bossId)
                .eq(AuditStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 查询
        List<AuditStoreEntity> auditStoreList = baseMapper.selectList(auditStoreLambdaQueryWrapper);
        if (auditStoreList.isEmpty()) {
            log.info("查询商店审核记录结果为空。businessId={}", businessId);
        }
        return auditStoreList;
    }

    @Override
    public AuditStoreEntity getAuditReject(Long storeId) {

        LambdaQueryWrapper<AuditStoreEntity> auditStoreLambdaQueryWrapper = new LambdaQueryWrapper<>();
        auditStoreLambdaQueryWrapper.eq(AuditStoreEntity::getStoreId, storeId)
                .eq(AuditStoreEntity::getStatus, CommonEnum.ONE.getValue())
                .eq(AuditStoreEntity::getDeleteStatus, CommonEnum.ZERO.getValue());
        // 根据id倒叙排序
        auditStoreLambdaQueryWrapper.orderByDesc(AuditStoreEntity::getId);
        // 查询一条数据
        auditStoreLambdaQueryWrapper.last(" LIMIT 1");
        // 查询
        List<AuditStoreEntity> auditStoreList = baseMapper.selectList(auditStoreLambdaQueryWrapper);
        if (auditStoreList.isEmpty()) {
            log.info("查询商店最新一条审核记录结果为空。storeId={}", storeId);
            return null;
        }
        return auditStoreList.get(0);
    }

    /**
     * 构建商店审核对象
     * @param auditSaveVo 参数对象
     * @return 商店审核对象
     */
    private AuditStoreEntity buildAuditStore(AuditSaveVo auditSaveVo) {
        UserPo userPo = SecurityUtils.getUser();
        Date date = new Date();

        AuditStoreEntity auditStore = new AuditStoreEntity();
        auditStore.setUserId(userPo.getUserId())
                .setStoreId(auditSaveVo.getStoreId())
                .setBusinessId(auditSaveVo.getBusinessId())
                .setAccountId(auditSaveVo.getAccountId())
                .setBossId(auditSaveVo.getBossId())
                .setType(auditSaveVo.getType())
                .setStatus(auditSaveVo.getStatus())
                .setAuditRemark(Optional.ofNullable(auditSaveVo.getAuditRemark()).orElse(StringUtils.EMPTY))
                .setDeleteStatus(CommonEnum.ZERO.getValue())
                .setCreateTime(date)
                .setCreateUser(userPo.getUsername());
        log.debug("商店审核对象：{}", auditStore);
        return auditStore;
    }
}